package com.ics.cmsadmin.modules.basic.service.impl;

import com.ics.cmsadmin.frame.core.bean.PageBean;
import com.ics.cmsadmin.frame.core.bean.PageResult;
import com.ics.cmsadmin.frame.core.enums.ApiResultEnum;
import com.ics.cmsadmin.frame.core.exception.CmsException;
import com.ics.cmsadmin.modules.basic.bean.StudentBean;
import com.ics.cmsadmin.modules.basic.bean.VideoHistoryBean;
import com.ics.cmsadmin.modules.basic.dao.VideoHistoryDao;
import com.ics.cmsadmin.modules.basic.service.StudentService;
import com.ics.cmsadmin.modules.basic.service.VideoHistoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VideoHistoryServiceImpl implements VideoHistoryService {

    @Resource
    private VideoHistoryDao videoHistoryDao;

    @Autowired
    private StudentService studentService;

    @Override
    public VideoHistoryBean findOne(String userId, String videoId) {
        return videoHistoryDao.findOne(userId, videoId);
    }

    @Override
    public VideoHistoryBean findByCourseId(String userId, String courseId) {
        if (StringUtils.isBlank(userId)) {
            throw new CmsException(ApiResultEnum.UN_LOGIN);
        }
        if (studentService.queryById(userId) == null) {
            throw new CmsException(ApiResultEnum.USER_NOT_EXIST);
        }
        return videoHistoryDao.findByCourseId(userId, courseId);
    }

    @Override
    public boolean save(VideoHistoryBean videoHistoryBean) {
        VideoHistoryBean existBean = videoHistoryDao.findOne(videoHistoryBean.getUserId(), videoHistoryBean.getVideoId());
        if (existBean != null) {
            return videoHistoryDao.update(existBean.getId(), videoHistoryBean) == 1;
        } else {
            return videoHistoryDao.insert(videoHistoryBean) == 1;
        }
    }

    /**
     * 获取最近学习视频5个
     * @param userId
     * @return
     */
    @Override
    public List<VideoHistoryBean> listByUserId(String userId) {
        return videoHistoryDao.list(VideoHistoryBean.builder().userId(userId).build(),
            new PageBean(1, 5));
    }

    @Override
    public PageResult list(VideoHistoryBean videoHistoryBean, PageBean page) {
        long count = videoHistoryDao.count(videoHistoryBean);
        if (count == 0){
            return new PageResult();
        }
        page = page == null ? new PageBean() : page;
        return PageResult.getPage(count, videoHistoryDao.list(videoHistoryBean, page));
    }

    @Override
    public VideoHistoryBean queryById(String id) {
        return videoHistoryDao.queryById(id);
    }

    @Override
    public boolean insert(VideoHistoryBean videoHistoryBean) {
        return videoHistoryDao.insert(videoHistoryBean) == 1;
    }

    @Override
    public boolean update(String id, VideoHistoryBean videoHistoryBean) {
        return videoHistoryDao.update(id, videoHistoryBean) == 1;
    }

    @Override
    public boolean delete(String id) {
        return videoHistoryDao.delete(id) == 1;
    }
}
